#include"string.h"
#include"debug.h"
#include"global.h"

void memset(void* dest,uint8_t value,uint32_t size){
	ASSERT(dest!=NULL);
	uint32_t i;
	char* d=dest;
	for(i=0;i<size;i++){
		d[i]=value;
	}
}

void memcpy(void* dest,void* src,uint32_t size){
	uint32_t i;
	ASSERT(dest!=NULL);
	ASSERT(src!=NULL);
	char *d,*s;
	d=dest;s=src;
	if(dest!=NULL && src!=NULL){
		for(i=0;i<size;i++){
			d[i]=s[i];
		}
	}
}

int memcmp(void* a, void* b, uint32_t size){
	uint32_t i;
	char* a_=a;
	char* b_=b;
	ASSERT(a!=NULL);
	ASSERT(b!=NULL);
	for(i=0;i<size;i++){
		if(a_[i]!=b_[i])
			return a_[i]-b_[i]>0 ? 1 : -1;
	}
	return 0;
}

char* strcpy(char* dest,const char* src){
	ASSERT(dest!=NULL && src!=NULL);
	char* d=dest;
	const char* s=src;
	while((*d++=*s++));
	return dest;
}

uint32_t strlen(const char* str){
	ASSERT(str!=NULL);
	const char* s=str;
	uint32_t i=0;
	while(*s++) i++;
	return i;
}


int8_t strcmp(const char* a,const char* b){
	const char* a_=a;
	const char* b_=b;
	while(*a_&&*b_){
		if(*a_!=*b_)
	       		return *a_>*b_? 1: -1;
		a_++;
		b_++;
	}
	return *a_==*b_ ? 0 : *a_>*b_ ? 1 : -1;
}

char* strchr(const char* str,const uint8_t ch){
	ASSERT(str!=NULL);
	while(*str!=0){
		if(*str==ch){
			return (char*)str;
		}
		str++;
	}
	return NULL;
}

char* strrchr(const char* str,const uint8_t ch){
	ASSERT(str!=NULL);
	const char* last;
	while(*str){
		if(*str==ch)
			last=str;
		str++;
	}
	return (char*)last;
}

char* strcat(char* dest,const char* src){
	ASSERT(dest!=NULL && src!=NULL);
	char* d=dest;
	while(*d++);d--;
	while((*d++=*src++));
	return dest;
}

uint32_t strchrs(const char* str,uint8_t ch){
	ASSERT(str!=NULL);
	const char* s=str;
	uint32_t cnt;
	while(*s){
		if(*s==ch)	
			cnt++;
		s++;
	}
	return cnt;
}
